package cn.sc.allinpay.allfavour.business.cashiertransonline;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import java.util.List;

import cn.pai.mvp.callback.MpCallBack;
import cn.pai.mvp.intervenor.Intervenor;
import cn.pai.mvp.presenter.PaiPresenter;
import cn.sc.allinpay.allfavour.app.Constant;
import cn.sc.allinpay.allfavour.business.cashiertrsdetailonline.CashierTransDetailOnlineActivity;
import cn.sc.allinpay.allfavour.business.common.data.OilListRecord;

/**
 * 交易流水 presenter
 * author：pany
 * on 2017/12/1 11:27
 */
public class CashierTransOnlinePresenter extends PaiPresenter<CashierTransOnlineContract.View> implements CashierTransOnlineContract.Presenter {

    public static final int REQ_TRANS_ONLINE_DETAIL = 0x01;

    private CashierTransOnlineModel model = new CashierTransOnlineModel();
    private boolean isQuerying = false;//是否正在查询，防止spinner引起的问题


    @Override
    protected Intervenor createIntervenor() {
        return new Intervenor() {
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
            }

            @Override
            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (Activity.RESULT_OK == resultCode) {
                    if (REQ_TRANS_ONLINE_DETAIL == requestCode) {
                        reqQueryOilList();
                    }
                }
            }
        };
    }

    @Override
    public void start() {
        reqQueryOilList();
    }

    @Override
    public void reqQueryOilList() {
        if (!isQuerying) {
            isQuerying = true;
            view.loading("数据查询中...");
            model.reqQueryOilList(view.getOrderNo(), new MpCallBack<List<OilListRecord>, String>() {
                @Override
                public void onSuccess(List<OilListRecord> resp) {
                    isQuerying = false;
                    view.loaded();
                    view.transAdapterChange(resp);
                }

                @Override
                public void onFailure(String response, String message) {
                    isQuerying = false;
                    view.loaded();
                    view.prompt(message);
                }
            });
        }
    }

    @Override
    public void startTransDetailActivity(OilListRecord record) {
        Bundle bundle = new Bundle();
        bundle.putString(Constant.BundleKey.TRANS_ONLINE_RECORD, record.getPayNum());
        view.toActivityForResult(CashierTransDetailOnlineActivity.class, bundle, REQ_TRANS_ONLINE_DETAIL);
    }
}
